package com.housekeeping.housekeeping.order.dao.impl;

import com.housekeeping.housekeeping.order.dao.ItemDao;
import com.housekeeping.housekeeping.order.entity.OrderItem;
import com.housekeeping.housekeeping.utils.DBManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ItemDaoImpl implements ItemDao {
    @Override
    public List<OrderItem> getItemList(String sid) throws SQLException {
        ArrayList<OrderItem> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        OrderItem orderItem = null;

        try {
            conn = DBManager.getConnection();
            String sql = "select * from order_item where sid = ?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, sid);
            rs = pStmt.executeQuery();
            while (rs.next()) {
                orderItem = new OrderItem();
                orderItem.setId(rs.getInt("id"));
                orderItem.setItemName(rs.getString("item_name"));
                orderItem.setAddress(rs.getString("address"));
                orderItem.setCount(rs.getInt("count"));
                orderItem.setStartTime(rs.getDate("start_time"));
                orderItem.setEndTime(rs.getDate("end_time"));
                orderItem.setPaidMoney(rs.getLong("paid_money"));
                orderItem.setSid(rs.getString("sid"));
                orderItem.setIsDelete(rs.getInt("is_delete"));

                list.add(orderItem);
            }
        } finally {
            DBManager.close(rs, pStmt, conn);
        }
        return list;
    }

    @Override
    public int getTotalItemNum(String sid) throws SQLException {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pStmt = null;
        int totalCount = 0;

        try {
            String sql = "select count(*) from order_item where sid = ?";
            conn = DBManager.getConnection();
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, sid);
            rs = pStmt.executeQuery();
            totalCount = rs.next() ? rs.getInt(1) : 0;
        } finally {
            DBManager.close(rs, pStmt, conn);
        }
        return totalCount;

    }

    @Override
    public List<OrderItem> getItemPageList(String sid, int page, int pageSize) throws SQLException {
        ArrayList<OrderItem> list = new ArrayList<>();

        Connection conn = null;
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        OrderItem orderItem = null;
        try {
            conn = DBManager.getConnection();
            String sql = "select * from order_item where sid = ? limit ?,?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, sid);
            pStmt.setInt(2, page);
            pStmt.setInt(3, pageSize);
            rs = pStmt.executeQuery();
            while (rs.next()) {
                orderItem = new OrderItem();
                orderItem.setId(rs.getInt("id"));
                orderItem.setItemName(rs.getString("item_name"));
                orderItem.setAddress(rs.getString("address"));
                orderItem.setCount(rs.getInt("count"));
                orderItem.setStartTime(rs.getDate("start_time"));
                orderItem.setEndTime(rs.getDate("end_time"));
                orderItem.setPaidMoney(rs.getLong("paid_money"));
                orderItem.setSid(rs.getString("sid"));
                orderItem.setIsDelete(rs.getInt("is_delete"));

                list.add(orderItem);
            }
        } finally {
            DBManager.close(rs, pStmt, conn);
        }

        return list;
    }
}
